﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ScFramework.Core;
using System;

public class Entity
{
    GameObject _gameObject;
    public GameObject gameObject => _gameObject;
    public int CompCnt => _components.Count;

    /// <summary>
    /// 
    /// </summary>
    protected bool _enable = false;

    protected bool _inited = false;
    /// <summary>
    /// 当前实体是否初始化.若为否,不能修改其动态数据
    /// </summary>
    public bool Inited
    {
        get => _inited;
    }

    /// <summary>
    /// 当前实体是否激活.若为否,其数据不会被同步
    /// </summary>
    public bool Enable
    {
        get => _enable;
    }
    EntityData _data;

    public EntityData Data
    {
        get => _data;
    }
    public UInt16 Identity
    {
        get
        {
            return _data.EntityIdentity.right();
        }
    }

    public UInt16 PrefabId
    {
        get => _data.EntityIdentity.left();
    }
    List<SComponent> _components = new List<SComponent>();


    public SComponent GetComponent(UInt16 id)
    {
        return _components[id];
    }
    //初始化物体
    public void Init(List<SComponent> comps,GameObject obj)
    {
        _enable = false;
        _components = comps;
        _gameObject = obj;
        
        foreach(var x in comps)
        {
            x.SInit();
        }
        _inited = true;

    }
    public EntityAPI entityAPI=null;
    public  void InitData(EntityData entityData,EntityAPI api)
    {
        entityAPI = api;
        _data = entityData;
        foreach(var x in _components)
        {
            for(ushort i = 0; i < x.ValueCnt; i++)
            {

          //      if (x.IsValuePure(i) == false)
                {
                    x.SetInstanceValue((ushort)i, x.GetValue((ushort)i));
                }
            }
            //将数据同步到物体上
        }

    }
    public void Start()
    {
        foreach(var x in _components)
        {
            x.SStart();
        }
    }
    public void SetEnable()
    {
        if (_enable == true)
        {
            return;
        }
        _enable = true;
        foreach(var comps in _components)
        {
            comps.gameObject.SetActive(true);
            comps.SOnEnable();
        }
    }
    public void SetDisable()
    {
        if (_enable == false)
        {
            return;
        }
        _enable = false;
        foreach(var comps in _components)
        {
            comps.gameObject.SetActive(false);
            comps.SOnDisable();
        }
    }

    public void FixedUpdate()
    {
        if (Enable)
        {
            foreach(var x in _components)
            {
                x.SFixedUpdate();
            }
        }
    }

    public void LateFixedUpdate()
    {
        if (Enable)
        {
            foreach(var x in _components)
            {
                x.SLateFixedUpdate();
            }
        }
    }


    public void Destory()
    {
        if (Enable)
        {
            SetDisable();
        }
        foreach(var comps in _components)
        {
            comps.SOnDestory();
        }
        
        _data = null;
        entityAPI = null;
    }
    public T GetComponent<T>()where T:SComponent
    {
        var ans = _components.Find(s => typeof(T).IsAssignableFrom(s.GetType()));
        if(ans== null)
        {
       //    Debug.LogWarning($"{this}: {typeof(T)} 组件未找到");
            return null;
        }
        else
        {
            return (T)ans;
        }
    }
    public List<T> GetComponents<T>() where T : SComponent
    {
        return _components.FindAll(s => typeof(T).IsAssignableFrom(s.GetType())).ConvertAll(s=>(T)s);

    }
}

